﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace Auktionshuset___Server
{
    internal class Server
    {
        private const int Port = 40404;

        private static void Main(string[] args)
        {
            UserMonitor userMonitor = new UserMonitor();
            AuctionMonitor auctionMonitor = new AuctionMonitor();

            User test = new User("test", "test", "Test");
            User test2 = new User("test2", "test", "Test2");
            userMonitor.Add(test);
            userMonitor.Add(test2);

            auctionMonitor.Add(new Auction
            {
                Title = "Auktion",
                Description = "Auktionsbeskrivelse",
                Owner = test,

                ImageBytes = new byte[0],

                NextBid = 1000,

                Starts = DateTime.Now,

                Active = true
            });

            EndPoint ep = new IPEndPoint(IPAddress.Any, Port);
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listener.Bind(ep);
            listener.Listen(10);

            List<Client> clients = new List<Client>();
            Broadcaster broadcaster = new Broadcaster(clients);

            while (true)
            {
                Socket socket = listener.Accept();

                Console.WriteLine("Client connected");

                Client client = new Client(socket, broadcaster, userMonitor, auctionMonitor);
                client.Start();

                clients.Add(client);
            }
        }
    }
}